package com.lin.androidgame21;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Build;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.security.PrivilegedAction;
import java.time.chrono.HijrahEra;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

class MyView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    private final SurfaceHolder holder;
    private final GestureDetector gestureDetector;
    private final float scaleX;
    private final float scaleY;
    private boolean isRun;
    private Paint paint;
    private Player player;
    private MyMap myMap;
    private ArrayList<NPC> npcs;
    private int collisionNPCID;
    private int talkIndex;
    private GameState gameState;
    private RectF rectF;
    private int step;
    private int progress;
    private Sprite sprite_yujianfeixing;
    private Combat combat;
    private MyMusic myMusic;

    public MyView(Context context) {
        super(context);
        holder = getHolder();
        holder.addCallback(this);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        Point screenSize = new Point();
        WindowManager windowManager = (WindowManager) context.getSystemService(
                Context.WINDOW_SERVICE
        );
        if (windowManager != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                windowManager.getDefaultDisplay().getRealSize(screenSize);
            } else {
                windowManager.getDefaultDisplay().getSize(screenSize);
            }
        }
        scaleX = (float) screenSize.x / 1280;
        scaleY = (float) screenSize.y / 768;
        init();
        gestureDetector = new GestureDetector(context,
                new GestureDetector.SimpleOnGestureListener() {
                    @Override
                    public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                            float distanceX, float distanceY) {
                        if (gameState != GameState.STORY || talkIndex != -1) {
                            return true;
                        }
                        if (Math.abs(distanceX) > Math.abs(distanceY)) {
                            if (distanceX > 0) {
                                move(Dir.LEFT);
                            } else {
                                move(Dir.RIGHT);
                            }
                        } else {
                            if (distanceY > 0) {
                                move(Dir.UP);
                            } else {
                                move(Dir.DOWN);
                            }
                        }
                        return super.onScroll(e1, e2, distanceX, distanceY);
                    }

                    @Override
                    public boolean onSingleTapConfirmed(MotionEvent e) {
                        singleTap(e);
                        return super.onSingleTapConfirmed(e);
                    }
                }
        );
    }

    private void singleTap(MotionEvent e) {
        switch (gameState) {
            case LOGO:
                gameState = GameState.CG;
                step = 200;
                break;
            case CG:
                gameState = GameState.MENU;
                break;
            case MENU:
                rectF.set(540 * scaleX, 460 * scaleY, 745 * scaleX, 500 * scaleY);
                if (rectF.contains(e.getX(), e.getY())) {
                    if (progress > 0) {
                        load0();
                    }
                    gameState = GameState.STORY;
                }
                rectF.set(540 * scaleX, 540 * scaleY, 745 * scaleX, 580 * scaleY);
                if (rectF.contains(e.getX(), e.getY())) {
                    if (progress == 1) {
                        load1();
                    }


                }
                rectF.set(540 * scaleX, 615 * scaleY, 745 * scaleX, 655 * scaleY);
                if (rectF.contains(e.getX(), e.getY())) {
                    ((MainActivity) getContext()).finish();
                }
                break;
            case STORY:
                if (talkIndex != -1) {
                    talkIndex++;
                    if (talkIndex == npcs.get(collisionNPCID).getTalks().size()) {
                        talkIndex = -1;
                    }
                } else {
                    rectF.set(0, 0, 192 * scaleX, 64 * scaleY);
                    if (rectF.contains(e.getX(), e.getY())) {
                        gameState = GameState.SET;
                    }
                }
                break;
            case SET:
                rectF.set(1240 * scaleX, 0, 1280 * scaleX, 40 * scaleY);
                if (rectF.contains(e.getX(), e.getY())) {
                    gameState = GameState.STORY;
                }
                break;
            case WORLD:
                rectF.set(960 * scaleX, 520 * scaleY, 1050 * scaleX, 588 * scaleY);
                if (progress == 1 && rectF.contains(e.getX(), e.getY())) {
                    sprite_yujianfeixing.setSpeedX(1);
                    sprite_yujianfeixing.setSpeedY(2);
                }
                rectF.set(1020 * scaleX, 540 * scaleY, 1175 * scaleX, 640 * scaleY);
                if (progress == 3 && rectF.contains(e.getX(), e.getY())) {
                    sprite_yujianfeixing.setSpeedX(1);
                    sprite_yujianfeixing.setSpeedY(1);
                }
                break;
            case COMBAT:
                combat.singleTap(e);
                break;
            case OVER:
                gameState = GameState.MENU;
                break;
            case WIN:
                load2();
                break;
        }
    }

    private void load0() {
        progress = 0;
        player.setPosition(640, 384);
        player.setDir(Dir.LEFT);
        player.setFrameSequence(player.getLeftFrameSequence());
        player.setFrameSequenceIndex(0);
        if (npcs != null) {
            for (NPC npc : npcs) {
                npc.move(-myMap.getX(), -myMap.getY());
            }
        }
        myMap.move(-myMap.getX(), -myMap.getY());
        collisionNPCID = -1;
        talkIndex = -1;
    }

    private void load1() {
        player.setPosition(1000, 384);
        collisionNPCID = -1;
        talkIndex = -1;
        gameState = GameState.STORY;

    }

    private void load2() {
        player.setPosition(640, 384);
        npcs.get(0).setTalks(getTalks("story.xml", "t006"));
        npcs.get(2).setTalks(getTalks("story.xml", "t007"));
        npcs.get(3).setTalks(getTalks("story.xml", "t008"));
        talkIndex = -1;
        collisionNPCID = -1;
        progress = 2;
        gameState = GameState.STORY;
    }

    private void move(Dir dir) {
        switch (dir) {
            case LEFT:
                if (player.getDir() != Dir.LEFT) {
                    player.setDir(Dir.LEFT);
                    player.setFrameSequence(player.getLeftFrameSequence());
                    player.setFrameSequenceIndex(0);
                } else {
                    player.nextFrame();
                    if (!player.footCollisionWith(myMap, -4, 0)) {
                        if (player.getX() + player.getWidth() / 2 > 640) {
                            player.move(-4, 0);
                        } else if (myMap.getX() < 0) {
                            myMap.move(4, 0);
                            if (npcs != null) {
                                for (NPC npc : npcs) {
                                    npc.move(4, 0);
                                }
                            }
                        } else {
                            player.move(-4, 0);
                        }
                    }
                }
                break;
            case RIGHT:
                if (player.getDir() != Dir.RIGHT) {
                    player.setDir(Dir.RIGHT);
                    player.setFrameSequence(player.getRightFrameSequence());
                    player.setFrameSequenceIndex(0);
                } else {
                    player.nextFrame();
                    if (!player.footCollisionWith(myMap, -4, 0)) {
                        if (player.getX() + player.getWidth() / 2 < 640) {
                            player.move(4, 0);
                        } else if (myMap.getX() > 1280 - myMap.getCols() * myMap.getWidth()) {
                            myMap.move(-4, 0);
                            if (npcs != null) {
                                for (NPC npc : npcs) {
                                    npc.move(-4, 0);
                                }
                            }
                        } else {
                            player.move(4, 0);
                        }
                    }
                }
                break;
            case UP:
                if (player.getDir() != Dir.UP) {
                    player.setDir(Dir.UP);
                    player.setFrameSequence(player.getUpFrameSequence());
                    player.setFrameSequenceIndex(0);
                } else {
                    player.nextFrame();
                    if (!player.footCollisionWith(myMap, 0, -4)) {
                        if (player.getY() + player.getHeight() / 2 > 384) {
                            player.move(0, -4);
                        } else if (myMap.getY() < 0) {
                            myMap.move(0, 4);
                            if (npcs != null) {
                                for (NPC npc : npcs) {
                                    npc.move(0, 4);
                                }
                            }
                        } else {
                            player.move(0, -4);
                        }
                    }
                }
                break;
            case DOWN:
                if (player.getDir() != Dir.DOWN) {
                    player.setDir(Dir.DOWN);
                    player.setFrameSequence(player.getDownFrameSequence());
                    player.setFrameSequenceIndex(0);
                } else {
                    player.nextFrame();
                    if (!player.footCollisionWith(myMap, 0, 4)) {
                        if (player.getY() + player.getHeight() / 2 < 384) {
                            player.move(0, 4);
                        } else if (myMap.getY() > 768 - myMap.getRows() * myMap.getHeight()) {
                            myMap.move(0, -4);
                            if (npcs != null) {
                                for (NPC npc : npcs) {
                                    npc.move(0, -4);
                                }
                            }
                        } else {
                            player.move(0, 4);
                        }
                    }
                }
                break;
        }

    }

    private void init() {
        myMusic = new MyMusic(getContext());
        gameState = GameState.LOGO;
        rectF = new RectF();
        step = 0;
        progress = 0;
        player = new Player(getBitmap("zhaolinger1.png"), 60, 104);
        player.setLeftFrameSequence(new int[]{4, 5, 6, 7});
        player.setRightFrameSequence(new int[]{8, 9, 10, 11});
        player.setUpFrameSequence(new int[]{12, 13, 14, 15});
        player.setDownFrameSequence(new int[]{0, 1, 2, 3});
        player.setFrameSequence(player.getLeftFrameSequence());
        player.setPosition(640, 390);
        player.setVisible(true);

        npcs = new ArrayList<>();
        NPC npc0 = new NPC(getBitmap("tiaosheng.png"), 136, 150);
        npc0.setTalks(getTalks("story.xml", "t000"));
        npc0.setPosition(0, 500);
        npc0.setVisible(true);
        npcs.add(npc0);
        NPC npc1 = new NPC(getBitmap("xiaogou.png"), 40, 40);
        npc1.setTalks(getTalks("story.xml", "t001"));
        npc1.setMove(new int[]{0, 1, 2, 3}, new int[]{12, 13, 14, 15}, 0, 10);
        npc1.setPosition(600, 600);
        npc1.setVisible(true);
        npcs.add(npc1);
        NPC npc2 = new NPC(getBitmap("suanming.png"), 52, 112);
        npc2.setTalks(getTalks("story.xml", "t002"));
        npc2.setMove(new int[]{0, 1, 2, 3}, new int[]{8, 9, 10, 11}, -20, 0);
        npc2.setPosition(1400, 500);
        npc2.setVisible(true);
        npcs.add(npc2);
        NPC npc3 = new NPC(getBitmap("lixiaoyao.png"), 60, 108);
        npc3.setTalks(getTalks("story.xml", "t003"));
        npc3.setMove(new int[]{4, 5, 6, 7}, new int[]{8, 9, 10, 11}, -10, 0);
        npc3.setPosition(1800, 200);
        npc3.setVisible(true);
        npcs.add(npc3);
        NPC npc4 = new NPC(getBitmap("dajia.png"), 148, 132);
        npc4.setTalks(getTalks("story.xml", "t004"));
        npc4.setPosition(2400, 800);
        npc4.setVisible(true);
        npcs.add(npc4);
        NPC npc5 = new NPC(getBitmap("chuansong.png"), 160, 296);
        npc5.setTalks(getTalks("story.xml", "t005"));
        npc5.setPosition(2450, 250);
        npc5.setVisible(true);
        npcs.add(npc5);
        collisionNPCID = -1;
        talkIndex = -1;

        myMap = new MyMap(getBitmap("cunzhuang.png"), 32, 32, 82, 30);
        int[][] map = {
                {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
                        17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
                        33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
                        49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
                        65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
                        81, 82},
                {83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98,
                        99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114,
                        115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130,
                        131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146,
                        147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162,
                        163, 164},
                {165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180,
                        181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
                        197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212,
                        213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228,
                        229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244,
                        245, 246},
                {247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262,
                        263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278,
                        279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294,
                        295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310,
                        311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326,
                        327, 328},
                {329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344,
                        345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360,
                        361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376,
                        377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392,
                        393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408,
                        409, 410},
                {411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426,
                        427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442,
                        443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458,
                        459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474,
                        475, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490,
                        491, 492},
                {493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508,
                        509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524,
                        525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540,
                        541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556,
                        557, 558, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572,
                        573, 574},
                {575, 576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590,
                        591, 592, 593, 594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 604, 605, 606,
                        607, 608, 609, 610, 611, 612, 613, 614, 615, 616, 617, 618, 619, 620, 621, 622,
                        623, 624, 625, 626, 627, 628, 629, 630, 631, 632, 633, 634, 635, 636, 637, 638,
                        639, 640, 641, 642, 643, 644, 645, 646, 647, 648, 649, 650, 651, 652, 653, 654,
                        655, 656},
                {657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671, 672,
                        673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688,
                        689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700, 701, 702, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 715, 716, 717, 718, 719, 720,
                        721, 722, 723, 724, 725, 726, 727, 728, 729, 730, 731, 732, 733, 734, 735, 736,
                        737, 738},
                {739, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749, 750, 751, 752, 753, 754,
                        755, 756, 757, 758, 759, 760, 761, 762, 763, 764, 765, 766, 767, 768, 769, 770,
                        771, 772, 773, 774, 775, 776, 777, 778, 779, 780, 781, 782, 783, 784, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 798, 799, 800, 801, 802,
                        803, 804, 805, 806, 807, 808, 809, 810, 811, 812, 813, 814, 815, 816, 817, 818,
                        819, 820},
                {821, 822, 823, 824, 825, 826, 827, 828, 829, 830, 831, 832, 833, 834, 835, 836,
                        837, 838, 839, 840, 841, 842, 843, 844, 0, 0, 847, 848, 849, 850, 851, 852,
                        853, 854, 855, 856, 857, 858, 859, 860, 861, 862, 863, 864, 865, 866, 867, 868,
                        869, 870, 871, 872, 0, 0, 0, 0, 0, 0, 0, 880, 881, 882, 883, 884,
                        885, 886, 887, 888, 889, 890, 891, 892, 893, 894, 895, 896, 897, 898, 899, 900,
                        901, 902},
                {903, 904, 905, 906, 907, 908, 909, 910, 911, 912, 913, 914, 915, 916, 917, 918,
                        919, 920, 921, 922, 923, 924, 925, 926, 0, 0, 0, 930, 931, 932, 933, 934,
                        935, 936, 937, 938, 939, 940, 941, 942, 943, 944, 945, 946, 947, 948, 949, 950,
                        951, 952, 953, 954, 0, 0, 0, 0, 0, 960, 961, 962, 963, 964, 965, 966,
                        967, 968, 969, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979, 980, 981, 982,
                        983, 984},
                {985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999, 1000,
                        1001, 1002, 1003, 1004, 1005, 1006, 0, 0, 0, 0, 0, 1012, 1013, 1014, 1015, 1016,
                        1017, 1018, 1019, 1020, 1021, 1022, 1023, 1024, 1025, 1026, 1027, 1028, 1029, 1030, 1031, 1032,
                        1033, 1034, 1035, 1036, 0, 0, 0, 0, 0, 1042, 1043, 1044, 1045, 1046, 1047, 1048,
                        1049, 1050, 1051, 1052, 1053, 1054, 1055, 1056, 1057, 1058, 1059, 1060, 1061, 1062, 1063, 1064,
                        1065, 1066},
                {1067, 1068, 1069, 1070, 1071, 1072, 1073, 0, 0, 0, 0, 1078, 1079, 1080, 1081, 1082,
                        1083, 1084, 1085, 1086, 1087, 0, 0, 0, 0, 0, 0, 1094, 1095, 1096, 1097, 1098,
                        1099, 1100, 1101, 1102, 1103, 1104, 1105, 1106, 1107, 1108, 1109, 1110, 1111, 1112, 1113, 1114,
                        1115, 1116, 1117, 1118, 0, 0, 0, 0, 0, 0, 1125, 1126, 1127, 1128, 1129, 1130,
                        1131, 1132, 1133, 1134, 1135, 1136, 0, 0, 0, 1140, 1141, 1142, 1143, 1144, 1145, 1146,
                        1147, 1148},
                {1149, 1150, 1151, 1152, 1153, 1154, 1155, 0, 0, 0, 0, 1160, 1161, 1162, 1163, 1164,
                        1165, 1166, 1167, 1168, 0, 0, 0, 0, 0, 0, 0, 1176, 1177, 1178, 1179, 1180,
                        1181, 1182, 1183, 1184, 1185, 1186, 1187, 1188, 1189, 1190, 1191, 1192, 0, 0, 1195, 1196,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 1214, 1215, 1216, 0, 0, 0, 0, 1221, 1222, 1223, 1224, 1225, 0, 0, 0,
                        0, 0},
                {1231, 1232, 1233, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1258, 1259, 1260, 1261, 1262,
                        1263, 1264, 1265, 1266, 1267, 1268, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0}
        };
        myMap.setTiledCell(map);
    }

    private ArrayList<Talk> getTalks(String fileName, String id) {
        ArrayList<Talk> talks = new ArrayList<>();
        try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Document document = documentBuilder.parse(getContext().getAssets().open(fileName));
            Element documentElement = document.getDocumentElement();
            NodeList nodeList1 = documentElement.getElementsByTagName("talks");
            for (int i = 0; i < nodeList1.getLength(); i++) {
                Element talksElement = (Element) nodeList1.item(i);
                if (id.equals(talksElement.getAttribute("id"))) {
                    NodeList nodeList2 = talksElement.getElementsByTagName("talk");
                    for (int j = 0; j < nodeList2.getLength(); j++) {
                        Element talkElement = (Element) nodeList2.item(j);
                        Talk talk = new Talk(talkElement.getAttribute("name"),
                                talkElement.getTextContent());
                        talks.add(talk);
                    }
                }
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return talks;
    }

    private Bitmap getBitmap(String path) {
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeStream(getContext().getAssets().open(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }


    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        isRun = true;
        new Thread(this).start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        isRun = false;
        myMusic.stop();
    }

    @Override
    public void run() {
        while (isRun) {
            long startTime = System.currentTimeMillis();
            logic();
            redraw();
            long endTime = System.currentTimeMillis();
            long diffTime = startTime - endTime;
            if (diffTime < 1000 / 60) {
                try {
                    Thread.sleep(1000 / 60 - diffTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void logic() {
        switch (gameState) {
            case LOGO:
                step += 5;
                if (step == 200) {
                    gameState = GameState.CG;
                }
                break;
            case CG:
                myMusic.play("CG.mid");
                step += 5;
                if (step == 1200) {
                    gameState = GameState.MENU;
                }
                break;
            case MENU:
                myMusic.play("menu.mp3");
                break;
            case STORY:
                myMusic.play("story.mp3");
                if (collisionWithNPC()) {
                    talkIndex = 0;
                }
                if (talkIndex == -1) {
                    if (npcs != null) {
                        for (NPC npc : npcs) {
                            npc.logic();
                        }
                    }
                }
                break;
            case WORLD:
                myMusic.play("yujianfeixing.wav");
                sprite_yujianfeixing.logic();
                sprite_yujianfeixing.nextFrame();
                if (progress == 1 && sprite_yujianfeixing.getY() > 520) {
                    gameState = GameState.COMBAT;
                    combat = new Combat(getContext(), scaleX, scaleY);
                    combat.setEnemyState(100, 100,
                            100, 100);
                    combat.setPlayerState1(50, 5,
                            50, 40);
                    combat.setPlayerState2(50, 6,
                            50, 30);
                    combat.setResultListener(new Combat.ResultListener() {
                        @Override
                        public void result() {
                            gameState = combat.getResult();
                            step = 0;
                        }
                    });
                }
                if (progress == 3 && sprite_yujianfeixing.getY() > 540) {
                    gameState = GameState.THANKS;
                    step = 0;
                }
                break;
            case COMBAT:
                myMusic.play("combat.mp3");
                combat.logic();
                break;
            case OVER:
                myMusic.play("gameover.wav");
                step += 5;
                if (step == 100)
                    gameState = GameState.MENU;
                break;
            case WIN:
                myMusic.play("win.wav");
                step += 5;
                if (step == 100) {
                    load2();
                }
                break;
            case THANKS:
                myMusic.play("thanks.mp3");
                step += 5;
                if (step == 1000)
                    ((MainActivity) getContext()).finish();
                break;
        }
    }

    private boolean collisionWithNPC() {
        if (npcs != null) {
            for (int i = 0; i < npcs.size(); i++) {
                if (i != collisionNPCID) {
                    NPC npc = npcs.get(i);
                    if (npc.collisionWith(player)) {
                        collisionNPCID = i;
                        if (i == 3) {
                            if (progress == 0) {
                                progress = 1;
                            }
                            if (progress == 2) {
                                progress = 3;
                            }
                        }
                        if ((progress == 1 || progress == 3) && i == 5) {
                            gameState = GameState.WORLD;
                            sprite_yujianfeixing = new Sprite(getBitmap("yujianfeixing.png"),
                                    64, 64);
                            sprite_yujianfeixing.setFrameSequence(new int[]{3, 4, 5, 6});
                            sprite_yujianfeixing.setPosition(960, 460);
                            sprite_yujianfeixing.setVisible(true);
                        }
                        return true;
                    }
                }
            }
        }
        return false;
    }

    private void redraw() {
        Canvas lockCanvas = holder.lockCanvas();
        try {
            synchronized (holder) {
                myDraw(lockCanvas);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (lockCanvas != null) {
                holder.unlockCanvasAndPost(lockCanvas);
            }
        }
    }

    private void myDraw(Canvas canvas) {
        canvas.save();
        canvas.scale(scaleX, scaleY);
        paint.reset();
        switch (gameState) {
            case LOGO:
                canvas.drawBitmap(getBitmap("logo.png"), 0, 0, paint);
                break;
            case CG:
                canvas.drawColor(Color.WHITE);
                canvas.save();
                canvas.clipRect(1280 - step, 0, 1280, 768);
                canvas.drawBitmap(getBitmap("cg.png"), 0, 0, paint);
                canvas.restore();
                break;
            case MENU:
                canvas.drawBitmap(getBitmap("menu.png"), 0, 0, paint);
                break;
            case STORY:
                myMap.draw(canvas);
                if (npcs != null) {
                    for (NPC npc : npcs) {
                        if (npc.getY() + npc.getHeight() < player.getY() + player.getHeight()) {
                            npc.draw(canvas);
                        }
                    }
                }
                player.draw(canvas);
                if (npcs != null) {
                    for (NPC npc : npcs) {
                        if (npc.getY() + npc.getHeight() >= player.getY() + player.getHeight()) {
                            npc.draw(canvas);
                        }
                    }
                }
                if (talkIndex != -1) {
                    Talk talk = npcs.get(collisionNPCID).getTalks().get(talkIndex);
                    talk.draw(canvas, paint);
                } else {
                    canvas.drawBitmap(getBitmap("player1.png"), 0, 0, null);
                }
                break;
            case SET:
                canvas.drawBitmap(getBitmap("set1.png"), 0, 0, null);
                canvas.drawBitmap(getBitmap("close.png"), 1240, 0, null);
                break;
            case WORLD:
                canvas.drawBitmap(getBitmap("world.png"), 0, 0, null);
                if (progress >= 1) {
                    canvas.drawBitmap(getBitmap("small_cunzhuang.png"),
                            960, 460, null);
                    canvas.drawBitmap(getBitmap("small_cunwai.png"),
                            960, 520, null);
                }
                if (progress >= 3) {
                    canvas.drawBitmap(getBitmap("small_shushan.png"),
                            1020, 540, null);
                }
                sprite_yujianfeixing.draw(canvas);
                break;
            case COMBAT:
                combat.draw(canvas, paint);
                break;
            case OVER:
                canvas.drawBitmap(getBitmap("combat_over.png"), 0, 0, null);
                break;
            case WIN:
                canvas.drawColor(Color.WHITE);
                canvas.drawBitmap(getBitmap("shengji1.png"), 200, 200, null);
                canvas.drawBitmap(getBitmap("shengji2.png"), 850, 200, null);
                canvas.drawBitmap(getBitmap("zhaolinger_happy.png"),
                        200, 300, null);
                canvas.drawBitmap(getBitmap("lixiaoyao_happy.png"),
                        850, 300, null);
                break;
            case THANKS:
                canvas.drawBitmap(getBitmap("suoyaota.png"), 0, 0, null);
                paint.setColor(Color.BLUE);
                paint.setTextAlign(Paint.Align.CENTER);
                paint.setTextSize(48);
                canvas.drawText("敬请期待后续开发", 640, 800 - step, paint);
                canvas.drawText("制作人：XXX", 640, 900 - step, paint);
                canvas.drawText("鸣谢:XXX", 640, 1000 - step, paint);

        }
        canvas.restore();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        gestureDetector.onTouchEvent(event);
        performClick();
        return true;
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }
}
